package com.huadao.pda.common;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;

import com.huadao.pda.R;


public class ProgressDialogHandler extends Handler {
    public static final int SHOW_PROGRESS_DIALOG=1;
    public static final int DISMISS_PROGRESS_DIALOG=2;

    private ProgressDialog progressDialog;

    private Context context;
    private boolean cancelable;

    public ProgressDialogHandler(Context context, boolean cancelable) {
        super();
        this.context = context;
        this.cancelable = cancelable;
    }

    private void initProgressDialog(String message){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(context);
            if(TextUtils.isEmpty(message)){
                message=context.getString(R.string.default_progress_dialog_message);
            }
            progressDialog.setMessage(message);
            progressDialog.setCancelable(cancelable);
            progressDialog.getWindow().setGravity(Gravity.CENTER);
        }
        if(!progressDialog.isShowing()){
            progressDialog.show();
        }
    }

    private void dismissProgressDialog(){
        if(progressDialog!=null){
            progressDialog.dismiss();
            progressDialog=null;
        }
    }

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what){
            case SHOW_PROGRESS_DIALOG:
                initProgressDialog((String) msg.obj);
                break;
            case DISMISS_PROGRESS_DIALOG:
                dismissProgressDialog();
                break;
        }
    }
}
